package com.shopx.auth.service.impl;


import com.shopx.auth.bean.SysUserAdd;
import com.shopx.auth.dao.db1.SysRoleDao;
import com.shopx.auth.dao.db1.SysUserAddDao;
import com.shopx.auth.model.SysUserAddModel;
import com.shopx.auth.model.SysUserRoleModel;
import com.shopx.auth.service.ISysUserAddService;
import com.shopx.auth.service.ISysUserRoleService;
import com.shopx.base.constants.ResultCodeEnum;
import com.shopx.base.exception.ShopxException;
import com.shopx.base.service.ISysSeqService;
import com.shopx.cmm.DTO.PageDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * TODO
 *
 * @author lituo
 * @className: SysUserAdd
 * @date 2022-02-04
 */
@Service
@Slf4j
public class SysUserAddServiceImpl extends BaseAuthService implements ISysUserAddService {

  @Autowired
  private SysUserAddDao sysUserAddDao;

  @Autowired
  private ISysUserRoleService sysUserRoleService;

  @Autowired
  private SysRoleDao sysRoleDao;


  @Autowired
  private ISysSeqService sysSeqService;

  @Override
  public SysUserAddModel selectByPrimaryKey(SysUserAdd sysUserAdd) {
    if (sysUserAdd == null) {
      throw new ShopxException(ResultCodeEnum.PAR_NULL);
    }
    return sysUserAddDao.selectByPrimaryKey(sysUserAdd);
  }


  @Override
  public SysUserAddModel selectOne(SysUserAdd sysUserAdd) {
    if (sysUserAdd == null) {
      throw new ShopxException(ResultCodeEnum.PAR_NULL);
    }
    return sysUserAddDao.selectOne(sysUserAdd);
  }

  @Override
  public int insertSelective(SysUserAdd sysUserAdd) {
    if (sysUserAdd == null) {
      throw new ShopxException(ResultCodeEnum.PAR_NULL);
    }
    int result = sysUserAddDao.insertSelective(sysUserAdd);
    if (result != 1) {
      throw new ShopxException(ResultCodeEnum.DB_ERROR);
    }
    return result;
  }

  @Override
  public int updateByPrimaryKeySelective(SysUserAdd sysUserAdd) {
    if (sysUserAdd == null) {
      throw new ShopxException(ResultCodeEnum.PAR_NULL);
    }
    int result = sysUserAddDao.updateByPrimaryKeySelective(sysUserAdd);
    if (result != 1) {
      throw new ShopxException(ResultCodeEnum.DB_ERROR);
    }
    return result;
  }

  @Override
  public int deleteByPrimaryKey(SysUserAdd sysUserAdd) {
    if (sysUserAdd == null) {
      throw new ShopxException(ResultCodeEnum.PAR_NULL);
    }
    int result = sysUserAddDao.deleteByPrimaryKey(sysUserAdd);
    if (result != 1) {
      throw new ShopxException(ResultCodeEnum.DB_ERROR);
    }
    return result;
  }

  @Override
  public int selectCount(SysUserAddModel sysUserAdd) {
    return sysUserAddDao.selectCount(sysUserAdd);
  }

  @Override
  public List<SysUserAddModel> selectList(SysUserAddModel sysUserAdd) {
    List<SysUserAddModel> list = sysUserAddDao.selectList(sysUserAdd);
    return list;
  }

  @Override
  public PageDTO<SysUserAddModel> getListPage(SysUserAddModel sysUserAdd) {
    if (sysUserAdd == null) {
      throw new ShopxException(ResultCodeEnum.PAR_NULL);
    }
    PageDTO<SysUserAddModel> pageDTO = new PageDTO<SysUserAddModel>();
    pageDTO.setiTotalDisplayRecords(selectCount(sysUserAdd));
    pageDTO.setData(selectList(sysUserAdd));
    return pageDTO;
  }

  @Override
  public PageDTO<SysUserAddModel> queryDeposit(SysUserAddModel sysUserAdd) {
    if (sysUserAdd == null) {
      throw new ShopxException(ResultCodeEnum.PAR_NULL);
    }
    PageDTO<SysUserAddModel> pageDTO = new PageDTO<SysUserAddModel>();
    sysUserAdd.setCpId(super.getUserCpId());
    pageDTO.setiTotalDisplayRecords(selectDepositCount(sysUserAdd));
    pageDTO.setData(selectDepositList(sysUserAdd));
    return pageDTO;
  }

  private int selectDepositCount(SysUserAddModel sysUserAdd) {
    return sysUserAddDao.selectDepositCount(sysUserAdd);
  }

  private List<SysUserAddModel> selectDepositList(SysUserAddModel sysUserAdd) {
    List<SysUserAddModel> list = sysUserAddDao.selectDepositList(sysUserAdd);
    for (SysUserAddModel user : list) {
      SysUserRoleModel sysUserRole = new SysUserRoleModel();
      sysUserRole.setUserId(user.getUserId());
      String roleNames = sysUserRoleService.selectRoleByUserId(sysUserRole);
      user.setRoleNames(roleNames);
    }
    return list;
  }


}

